//
//  MyBids.swift
//  LeGou
//
//  Created by lxm on 2022/10/17.
//

import UIKit

class MyCompoundRecrod: ZMViewController, UITableViewDelegate, UITableViewDataSource, ListRequestObjct {

    //MARK:--------------------属性--------------------
    var listData = [AuctionModels.ConflateRecord]()
    var page = 1
    var pageSize = 30
    var refreshScrollView : UIScrollView {
        tableView
    }
    var ahGoodsRecordId = 0
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var tableView: UITableView!
        //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        getlist()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "我的合成记录"
        tableView.delegate = self
        tableView.dataSource = self
        tableView.zm.register(nibName: "MyBidsCell", reuserId: "MyBidsCell")
        tableView.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0)
        addRefresh(scrollView: tableView) { [weak self] refresh in
            guard let self = self else {return}
            if refresh {
                self.page = 1
            }
            self.getlist()
        }
        addNodataScrollView(scrollView: tableView)
        emptyTitle = "暂无数据"
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {

    }

    func getlist() {
        ZMUtil.listRequest(type: AuctionModels.ConflateRecord.self, api: AuctionAPI.getConflateRecord(["productId" : ahGoodsRecordId, "limit" : pageSize, "page" : page, "userId" : ZMAppShareData.share.safeUserId])) { resp in
            self.listRequest(resp)
        }
    }
    
    //MARK:--------------------delegate--------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0 || tableView.mj_footer?.state == .noMoreData
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyBidsCell") as! MyBidsCell
        let data = listData[indexPath.row]
     
        
        cell.textColor = "333333".zm.hex()
        cell.one.text = data.conflateTime.zm.stringToString(toFormate: "MM.dd HH:mm:ss")
        
        cell.two.text = "合成成功"
        cell.three.text = "¥\(data.amount.zm.roundingDown(keep: 2))"
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        guard let cell = cell as? MyBidsCell else {return}
        let allCount = self.listData.count
        let current = indexPath.row
        if allCount == 1 {
            ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 8)
            cell.topH.constant = 12
            cell.bottomH.constant = 12
        } else {
            if current == 0 {
                ZMUtil.clipCircle(view: cell, Corner: [.topLeft, .topRight,], radius: 8)
                cell.topH.constant = 12
                cell.bottomH.constant = 4
            } else if current == allCount - 1 {
                ZMUtil.clipCircle(view: cell, Corner: [.bottomLeft, .bottomRight], radius: 8)
                cell.topH.constant = 4
                cell.bottomH.constant = 12
            } else {
                cell.layer.mask = nil
                cell.topH.constant = 4
                cell.bottomH.constant = 4
            }
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let allCount = self.listData.count
        let current = indexPath.row
        if allCount == 1 {
            return 17 + 24
        } else {
            if current == 0 {
                return 17 + 12 + 4
            } else if current == allCount - 1 {
                return 17 + 12 + 4
            } else {
                return 17 + 8
            }
        }
    }
}

